
const localData = localStorage.getItem('pz_v3pz');


const state = localData ? localData.menu : {
	isCollapse: false,
	selectMenu: [],
	routerList: [],
	menuActive: '1-1',
};

const mutations = {
	collapseMenu(state) {
		state.isCollapse = !state.isCollapse;
	},
	addMenu(state, payload) {
		// 对数据标签进行去重
		if (state.selectMenu.findIndex(item => item.path === payload.path) === -1) {
			state.selectMenu.push(payload);
		}
	},
	closeMenu(state, payload) {
		// 查找点击的数据的索引
		const index = state.selectMenu.findIndex(val => val.name === payload.name);
		// 通过索引删除数组指定元素
		// splice有四个参数，第一个是索引起点，第二个是删除的元素个数，后面的参数则是添加的元素
		// 即可以同时删除和添加元素
		/* 删除该索引的一个元素 */
		state.selectMenu.splice(index, 1);
	},
	dynamicMenu(state, payload) {
		// 通过glob导入文件
		const modules = import.meta.glob('../views/**/**/*.vue');
		function routerSet(router) {
			router.forEach(route => {
				// 判断有没有子菜单，拼接路由数据
				if (!route.children) {
					const url = `../views${route.meta.path}/index.vue`;
					// 拿到获取的vue组件
					route.component = modules[url];
				}
			});
		}
		routerSet(payload);
		// 拿到完整的路由
		state.routerList = payload;
	},

	updateMenuActive(state, payload) {
		state.menuActive = payload;
	}
};

export default {
	state,
	mutations
};